import {request} from "../../request/index.js";
Page({
    data:{
        tabs:[
            {
                id:0,
                name:"综合",
                isActive:true
            },
            {
                id:1,
                name:"销量",
                isActive:false
            },
            {
                id:2,
                name:"价格",
                isActive:false
            }
        ],
        goodsList:[]
    },
    //用以请求商品分裂的接口参数
    queryParams:{
        query:"",
        cid:"",
        pagenum:1,
        pagesize:10
    },
    //总页码
    totalPageNumber:0,
    onLoad:function(option){
        this.queryParams.cid=option.cat_id || "";
        this.queryParams.query=option.query || "";
        this.getGoodsList(this.queryParams);
    },
    //点击子组件触发的父组件上的自定义事件
    clickChildTab(e){
        // console.log(e);
        this.data.tabs.forEach((item)=>{
            if(item.id===e.detail.index){
                item.isActive=true;
            }else{
                item.isActive=false;
            };
        });
        // this.data.tabs.forEach(item=>item.id===e.detail.index?item.isActive=true:item.isActive=false);
        this.setData({
            tabs:this.data.tabs
        });
    },
    //初始化获取商品列表事件监听
    async getGoodsList(params){
        let res=await request({url:"/goods/search",data:params});
        this.totalPageNumber=Math.ceil(res.data.message.total/this.queryParams.pagesize);//获取总页码
        this.setData({
            goodsList:[...this.data.goodsList,...res.data.message.goods]
        });
        wx.stopPullDownRefresh({});
    },
    //页面上拉触底事件监听
    onReachBottom(){
        if(this.queryParams.pagenum>=this.totalPageNumber){//判断是否还有下一页数据
            wx.showToast({
                title: '数据已加载完毕',
                duration:2000,
                icon:"success"
            });
        }else{
            this.queryParams.pagenum++;
            this.getGoodsList(this.queryParams);
        };
    },
    //页面下拉刷新事件监听
    onPullDownRefresh(){
        this.setData({
            goodsList:[]
        });
        this.queryParams.pagenum=1;
        this.getGoodsList(this.queryParams);
    }
})